<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-table border v-loading="loading" :data="columns" @selection-change="handleSelectionChange"
                  :max-height="tableMaxHeight">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="产品编码" align="center" prop="code"/>
          <el-table-column label="产品名称" align="center" prop="name" show-overflow-tooltip/>
         <!-- <el-table-column label="产品品牌" align="center" prop="brand"/>-->
          <el-table-column label="产品型号" align="center" prop="model"/>
          <el-table-column label="产品明细" align="center" prop="brand"/>
          <el-table-column label="产品单位" align="center" prop="unit"/>
          <!--<el-table-column label="含税单价(成本)" align="center" prop="costPriceTax"/>-->
          <el-table-column label="销售单价(成本)" width="210">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.costPriceTax" :precision="2"
                               :min="0"></el-input-number>
            </template>
          </el-table-column>
          <!--<el-table-column label="订单数量" align="center" prop="counts" width="210">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.counts"
                               :min="0" @change="handleChangeCounts(scope.row)"></el-input-number>
            </template>
          </el-table-column>-->
          <!--<el-table-column label="采购数量" align="center" prop="count"/>-->
          <el-table-column label="库存数量" align="center" prop="remainderCount"/>
          <el-table-column label="销售单价" width="210">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.goodsUnitPrice" :precision="2"
                               :min="scope.row.costPriceTax" @change="handleChangeMoney(scope.row)"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="销售数量" width="210">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.goodsCount" :max="scope.row.remainderCount"
                               :min="0" @change="handleChangeMoney(scope.row)" ></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="销售总价" align="center" prop="goodsTotalPrice"/>
         <!-- <el-table-column label="销售总价" width="210">
            <template slot-scope="scope">
              <el-input-number disabled v-model="scope.row.goodsTotalPrice" :min="0"></el-input-number>
            </template>
          </el-table-column>-->

        <!--  <el-table-column label="剩余数量" width="210">
            <template slot-scope="scope">
              <el-input-number disabled v-model="scope.row.shengyu" :min="0"></el-input-number>
            </template>
          </el-table-column>-->
          <el-table-column label="发生日期" width="200">
            <template slot-scope="scope">
              <el-date-picker clearable style="width: 160px"
                              v-model="scope.row.buyDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择发生日期">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {addAction, deleteAction, getAction, getById, updateAction} from '@/api/manage'
    import {insertGoodsSale} from '@/api/develop/goodsSale'
    // import { updateGoodsSale } from '@/api/develop/goodsProject'
    export default {
        name: "PleaseBuyOrderinChose",
        dicts: ['goods_customer', 'company', 'yes_no', 'buy_type'],
        components: {},
        data() {
            return {
                // 遮罩层
                title: "产品选择",
                loading: true,
                open: false,
                // 选中数组
                ids: [],
                proId: "",
                saleType: "",
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 入库信息管理表格数据
                dataList: [],
                columns: [],
                form: {},
                rules: {},
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: null,
                    buyName: null,
                    buyDate: null,
                    buyType: null,
                    reimburse: null,
                    supplierId: null,
                    supplierName: null,
                    params: {
                        dataFlag: "1"
                    }
                },
                url: {
                    list: '/goodsOrderIn/goodsOrderIn/list',
                    remove: '/goodsOrderIn/goodsOrderIn/',
                    export: '/goodsOrderIn/goodsOrderIn/export',
                },
                tableMaxHeight: window.innerHeight - 400,
            };
        },
        created() {

        },
        methods: {
          handleChangeCounts(row) {
            let that = this;
            let handleReckonMoney = (row) => {
              if(row.shengyu ==undefined || row.shengyu ==null ){
                let row_count = 0;
                row_count =row.counts- row.goodsCount;
                that.$set(row, 'shengyu', row_count);
              }else{
                let row_count =row.shengyu- row.goodsCount;
                that.$set(row, 'counts', row_count);
              }
            };
            handleReckonMoney(row);
          },
            handleChangeMoney(row) {
                let that = this;
                let handleReckonMoney = (row) => {
                    let row_money = 0;
                    row_money = that.functionNumberMul(Number(row.goodsUnitPrice), Number(row.goodsCount))
                    that.$set(row, 'goodsTotalPrice', row_money);
                };
                handleReckonMoney(row);
            },
            functionNumberMul(arg1, arg2) {
                var m = 0;
                var s1 = arg1.toString();
                var s2 = arg2.toString();
                try {
                    m += s1.split(".")[1].length;
                } catch (e) {
                }
                try {
                    m += s2.split(".")[1].length;
                } catch (e) {
                }
                return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
            },
            chose(proId, type) {
                this.open = true;
                this.proId = proId;
                this.saleType = type
                this.getList();
            },
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    let res = response.rows;
                    for (let i = 0; i < res.length; i++) {
                        res[i].buyDate = new Date();
                    }
                    this.columns = res;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 取消按钮
            cancel() {
                this.open = false;
                // this.reset();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.dataList = selection;
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 提交按钮 */
            submitForm() {
                let that = this;
                const ids = this.ids;
                if (ids == null || ids == '' || ids == 'undefined') {
                    that.$modal.msgError("请选择一条记录");
                    // alert("请选择一条记录");
                } else {
                    that.$refs["form"].validate(valid => {
                        if (valid) {
                            const goodsOrderIn = Object.assign({});
                            goodsOrderIn.projectId = this.proId;
                            goodsOrderIn.type = this.saleType;
                            //goodsOrderIn.ids=ids.toString();
                            goodsOrderIn.columns = that.columns;
                            addAction(this.url.pleaseBuyForPrj, goodsOrderIn).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                            /*insertGoodsSale(goodsOrderIn).then(res => {
                              that.$modal.msgSuccess(res.msg);
                              if (res.code === 200) {
                                that.$modal.msgSuccess("修改完成！");
                                that.open = false;
                                that.$emit('ok');
                              }
                            });*/
                        }
                    });
                    /*const goodsOrderIn = Object.assign({});
                    goodsOrderIn.projectId = this.proId;
                    goodsOrderIn.ids=ids.toString();
                    insertGoodsSale(goodsOrderIn).then(res => {
                      if (res.code === 200) {
                        this.open = false;
                        this.$emit('ok');
                      }
                    });*/
                }
            },
        }
    };
</script>
